package main

import (
	"log"
	"sync"
)

type UserAges struct {
	ages map[string]int
	sync.Mutex
}

func (ua *UserAges) Add(name string, age int) {
	ua.Lock()
	defer ua.Unlock()
	ua.ages[name] = age
}
func (ua *UserAges) Get(name string) int {
	if age, ok := ua.ages[name]; ok {
		return age
	}
	return -1
}

func main() {
	us := UserAges{ages: make(map[string]int)}
	wt := sync.WaitGroup{}
	wt.Add(2)
	go func() {
		defer func() {
			wt.Done()
		}()
		for i := 0; i < 100; i++ {
			us.Add("zack", i)
		}
	}()

	go func() {
		defer func() {
			wt.Done()
		}()
		for i := 0; i < 100; i++ {
			log.Println(us.Get("zack"))
		}
	}()

	wt.Wait()
}
